package com.lajiaomian.springbootruijiwaimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lajiaomian.springbootruijiwaimai.common.BaseContext;
import com.lajiaomian.springbootruijiwaimai.common.R;
import com.lajiaomian.springbootruijiwaimai.pojo.AddressBook;
import com.lajiaomian.springbootruijiwaimai.pojo.User;
import com.lajiaomian.springbootruijiwaimai.service.impl.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.file.Watchable;
import java.util.List;

/**
 * @author lajiaomian
 * @create 2022-08-17-13:41
 */
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取用户地址列表
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> AList(HttpSession session){
        log.info("地址簿。。。:{}");
        //先通过session获取当前登录的对象
        Long userId = (Long) session.getAttribute("user");
        //获取这个对象的id，然后通过user_id来查询地址列表
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null!=userId,AddressBook::getUserId,userId);
        List<AddressBook> addressBookList = addressBookService.list(wrapper);
        //将列表返回
        return R.success(addressBookList);
    }

    /**
     * 更改默认地址
     * @param addressBook
     * @return
     */
    @Transactional
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址，对象为：{}",addressBook.toString());
        //查询isDefault为1的那条地址，把isDefault改为0
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook before = addressBookService.getOne(wrapper);
        before.setIsDefault(0);
        addressBookService.updateById(before);
        //将传过来的地址的isDefault设置为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }

    /**
     * 添加收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrId());
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 根据id查询对应信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable("id") String id){
        log.info("id:{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址详情
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> get(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1).eq(AddressBook::getUserId,BaseContext.getCurrId());
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);
    }
}
